﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        IEnumerable<PROJECTCLASS> projectClass = new PCDataContext().GetTable<PROJECTCLASS>();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                //BindCategoryTreeViews(tvRoot, true, "0");
                TreeNode node = new TreeNode();
                AddTree(0, node);
              

            }

        }

        public DataTable GetRootData()
        {


            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBTestConnectionString"].ConnectionString);
            string sql = "select * from PROJECTCLASS";
            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            DataTable dt = null;
            try
            {
                dt = new DataTable();
                conn.Open();
                da.Fill(dt);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message, ex);
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
            return dt;

        }

        public void AddTree(int ID, TreeNode pNode)
        {

            var projectClassAll = projectClass.Where(p => p.PC_PARENT == ID).Select(s => s);

            foreach (var item in projectClassAll)
            {
                TreeNode Node = new TreeNode();
                //处理主节点
                if (ID == 0)
                {
                    Node.Value = item.PC_ID.ToString();
                    Node.Text = item.PC_NAME.ToString();
                    tvRoot.Nodes.Add(Node);    //加入主TreeView树浏览
                    AddTree(item.PC_ID, Node); //递归遍历
                }
                else
                {
                    //处理子节点
                    Node.Value = item.PC_ID.ToString();
                    Node.Text = item.PC_NAME.ToString();
                    pNode.ChildNodes.Add(Node);
                    AddTree(item.PC_ID, Node); //递归遍历
                }
            }

           
        }


        private void CreateChildNodes(TreeNode rootNode, DataTable dataTable, bool isExpand, string sSelectedData)
        {
            //DataRow[] rows = dataTable.Select("parentID='" + rootNode.Value + "'", "orderby desc");
            DataRow[] rows = dataTable.Select(string.Format("PC_PARENT='{0}'", rootNode.Value));


            foreach (DataRow row in rows)
            {
                TreeNode node = new TreeNode();
                node.Text = row["PC_NAME"].ToString();
                node.Value = row["PC_ID"].ToString();
                if (node.Value == sSelectedData)
                {
                    node.Selected = true;
                }
                node.Expanded = isExpand;
                rootNode.ChildNodes.Add(node);
                CreateChildNodes(node, dataTable, isExpand, sSelectedData);
            }

        }

        /// <summary>
        /// 绑定全部商品类别到TreeView
        /// </summary>
        /// <param name="tv">TreeView控件</param>
        /// <param name="isExpand">是否展开子节点</param>
        /// <param name="sSelectedData">被选择中的节点的值</param>
        protected void BindCategoryTreeViews(TreeView tv, bool isExpand, string sSelectedData)
        {
            try
            {

                DataTable dataTable = GetRootData();
                tv.Nodes.Clear();
                DataRow[] rowList = dataTable.Select("PC_PARENT='0'");
                if (rowList.Length <= 0)
                    return;
                TreeNode rootNode = new TreeNode();
                rootNode.Text = rowList[0]["PC_NAME"].ToString();
                rootNode.Value = rowList[0]["PC_ID"].ToString();
                rootNode.Expanded = isExpand;
                rootNode.Selected = true;
                tv.Nodes.Add(rootNode);
                //创建其它节点
                CreateChildNodes(rootNode, dataTable, isExpand, sSelectedData);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }



    }
}